//! 物品配置
use core::fmt;
use std::collections::HashMap;

use bevy::{ecs::resource::Resource, prelude::{Deref, DerefMut}};
use bincode::{Decode, Encode};

///工具类型
#[derive(Clone, Encode, Decode, PartialEq, Debug)]
pub enum ToolType {
    ///锄头
    Hoe,
    ///铲子
    Shovel,
    ///水壶
    WateringCan,
    ///镰刀
    Sickle,
}

/// 作物配置
#[derive(Clone, Encode, Decode, PartialEq, Debug, Default)]
pub struct SeedItem {
    ///作物唯一id
    pub mid: u32,
    ///作物资源
    pub resource_path: String,
    ///作物资源多少行
    pub rows: u32,
    ///作物资源多少列
    pub columns: u32,
    ///作物有多少阶段
    pub stages_num: u32,
    ///到下一个阶段需要多少天
    pub next_day: u32,
}

///家具类型
#[derive(Clone, Encode, Decode, PartialEq, Debug)]
pub enum FurnitureType {
    ///床
    Bed,
}

///家具
#[derive(Clone, Encode, Decode, PartialEq, Debug)]
pub struct FurnitureItem {
    ///家具唯一id
    pub mid: u32,
    ///家具资源
    pub resource_path: String,
    ///家具类型
    pub furniture_type: FurnitureType,
}

/// 物品类型
#[derive(Clone, Encode, Decode, PartialEq, Debug)]
pub enum GoodsType {
    ///普通物品
    Simple,
    ///种子
    Seed(SeedItem),
    ///工具
    Tool(ToolType),
    ///家具
    Furniture(FurnitureItem),
}

impl fmt::Display for GoodsType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GoodsType::Simple => write!(f, ""),
            GoodsType::Seed(_) => write!(f, "种植"),
            GoodsType::Tool(tool) => match tool {
                ToolType::Hoe => write!(f, "锄地"),
                ToolType::Shovel => write!(f, "铲除作物"),
                ToolType::WateringCan => write!(f, "浇水"),
                ToolType::Sickle => write!(f, "收割"),
            },
            GoodsType::Furniture(_) => write!(f, "放置"),
        }
    }
}

/// 作物配置
#[derive(Clone, Encode, Decode, PartialEq, Debug)]
pub struct GoodsItem {
    ///物品唯一id
    pub mid: u32,
    ///物品名
    pub name: String,
    ///物品icon路径
    pub _icon_path: String,
    ///物品类型
    pub goods_type: GoodsType,
}

impl GoodsItem {
    pub fn new(mid: u32, name: &str, icon_path: &str, goods_type: GoodsType) -> Self {
        GoodsItem {
            mid,
            name: name.into(),
            _icon_path: icon_path.into(),
            goods_type,
        }
    }
}

impl fmt::Display for GoodsItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

#[derive(Resource, Deref, DerefMut, Debug)]
pub struct GoodsConfig {
    pub configs: HashMap<u32, GoodsItem>,
}

impl GoodsConfig {
    pub fn new() -> Self {
        GoodsConfig {
            configs: HashMap::new(),
        }
    }

    fn add(&mut self, item: GoodsItem) -> &mut Self {
        let config = self.configs.get(&item.mid);
        if config.is_none() {
            self.configs.insert(item.mid, item);
        } else {
            print!("已经存在mid:{}name:{} 物品的配置", item.mid, item.name);
        }
        self
    }

    pub fn get_config(&self, mid: u32) -> Option<&GoodsItem> {
        self.configs.get(&mid)
    }
}

///获取种子配置
pub fn get_goods_config() -> GoodsConfig {
    let mut crop_config = GoodsConfig::new();
    crop_config
        .add(GoodsItem::new(
            0,
            "葫芦卜种子",
            "",
            GoodsType::Seed(SeedItem {
                mid: 0,
                resource_path: "texture/seed/carrot.png".into(),
                rows: 3,
                columns: 1,
                stages_num: 3,
                next_day: 2,
            }),
        ))
        .add(GoodsItem::new(
            1,
            "白菜种子",
            "",
            GoodsType::Seed(SeedItem {
                mid: 1,
                resource_path: "texture/seed/cabbage.png".into(),
                rows: 3,
                columns: 1,
                stages_num: 3,
                next_day: 2,
            }),
        ))
        .add(GoodsItem::new(2, "葫芦卜", "", GoodsType::Simple))
        .add(GoodsItem::new(3, "白菜", "", GoodsType::Simple))
        .add(GoodsItem::new(
            4,
            "锄头",
            "",
            GoodsType::Tool(ToolType::Hoe),
        ))
        .add(GoodsItem::new(
            5,
            "铲子",
            "",
            GoodsType::Tool(ToolType::Shovel),
        ))
        .add(GoodsItem::new(
            6,
            "水壶",
            "",
            GoodsType::Tool(ToolType::WateringCan),
        ))
        .add(GoodsItem::new(
            7,
            "镰刀",
            "",
            GoodsType::Tool(ToolType::Sickle),
        ))
        .add(GoodsItem::new(
            8,
            "很普通的床",
            "",
            GoodsType::Furniture(FurnitureItem {
                mid: 8,
                resource_path: "texture/furniture/bed.png".into(),
                furniture_type: FurnitureType::Bed,
            }),
        ));
    crop_config
}
